<?php

namespace app\admin\controller\fzdc;

use app\common\controller\Backend;

/**
 * Ajax
 *
 * @icon fa fa-circle-o
 * @internal
 */
class Base extends Backend
{

    protected $authHouseIds = null;
    protected $authStoreIds = null;
    protected $addonsConfig = null;
    protected $authMerchantIds = null;

    protected $dataLimit = 'auth';

    public function _initialize()
    {
        parent::_initialize();

        $this->addonsConfig = get_addon_config('fzdc');

        $this->dataLimit = $this->addonsConfig['authModel'];

        //增加一个商户权限麻烦写一下
       

        if($this->adminuserinfo['merchant_id']>0){
           
              

            if ($this->addonsConfig['isStoreAllocate']==1) {
                    //证明是指定的门店
                // $adminIds = parent::getDataLimitAdminIds();
                $adminIds = $this->auth->id;
             
                //查找当前登录的用户是不是指定了用户

                $is_store_admin=(new \app\admin\model\fzdc\store\StoreAdmin())->where('admin_id', 'in', $adminIds)->find();
                
                if(empty($is_store_admin)){
                    //证明没找到是商家

                    $storeAdminList = (new \app\admin\model\fzdc\store\Store())
                    // ->field("sa.id,sa.store_ids,GROUP_CONCAT(s.id) seat_ids")
                    ->field("sa.id,sa.merchant_id")
                    ->alias('sa')
                    // ->join('fzdc_seat s', "FIND_IN_SET(s.store_id,sa.store_ids)", 'LEFT')
                    ->where('merchant_id', 'in', $this->adminuserinfo['merchant_id'])
                    ->group('sa.id')
                    ->select();
    
                    $storeIds = [];
                    $seatIds = [];
                    $merchantIds = [];
        
                    foreach ($storeAdminList as $res) {
                        $storeIds[]= $res['id'];
                        // $seatIds = array_merge($seatIds, explode(',', $res['seat_ids']));
        
                        // $merchantIds = array_merge($merchantIds, explode(',', $res['merchant_ids']));
                    }
                    
        
                    $this->authStoreIds = array_filter(array_unique($storeIds));
                    // $this->authHouseIds = array_filter(array_unique($seatIds));


                }else{
                    //证明是门店

                    $storeAdminList = (new \app\admin\model\fzdc\store\StoreAdmin())
                    ->field("sa.id,sa.store_ids,GROUP_CONCAT(s.id) seat_ids")
                    ->alias('sa')
                    ->join('fzdc_seat s', "FIND_IN_SET(s.store_id,sa.store_ids)", 'LEFT')
                    ->where('admin_id', 'in', $adminIds)
                    ->group('sa.id')
                    ->select();
    
                    $storeIds = [];
                    $seatIds = [];
                    $merchantIds = [];
        
                    foreach ($storeAdminList as $res) {
                        $storeIds = array_merge($storeIds, explode(',', $res['store_ids']));
                        $seatIds = array_merge($seatIds, explode(',', $res['seat_ids']));
        
                        // $merchantIds = array_merge($merchantIds, explode(',', $res['merchant_ids']));
                    }
                    
        
                    $this->authStoreIds = array_filter(array_unique($storeIds));
                    $this->authHouseIds = array_filter(array_unique($seatIds));
                }
                
                // $this->authMerchantIds = array_filter(array_unique($merchantIds)); 
            }

             //证明是商户了
            //增加一个当前商户的条件
            $this->authStoreIds = array_filter(array_unique($storeIds));

        }else{
            // admin管理员
        }
    


      
    }
}






